<?php

class User_IndexController extends My_Controller
{
	public $sh_ajaxian = array('save');
    
	public function init()
	{
		$this->UserModel = new User_Model_User();  
		$this->auth = Zend_Auth::getInstance();
	}
    
	public function loginAction()
    {
    	if(!empty($this->params['login']) && !empty($this->params['password']))
    	{
	    	$login = $this->params['login'];
	    	$password = $this->params['password'];
	
			if($login=='') {$this->view->assign('users_message','Enter Username!'); $this->_forward('index','index','default'); return;}
			if($password=='') {$this->view->assign('users_message','Enter Password!'); $this->_forward('index','index','default'); return;}
			
	     	$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Registry::get('db'));
	     	$authAdapter->setTableName('users')
		                  ->setIdentityColumn('username')
		                  ->setCredentialColumn('password')
		                  ->setIdentity($login)
		                  ->setCredential(md5($password));
		    $zendAuth = Zend_Auth::getInstance();
	        $result = $zendAuth->authenticate($authAdapter);

	      if($result->isValid()) 
	      {
	         $data = $authAdapter->getResultRowObject(null, 'password');
	         $zendAuth->getStorage()->write($data);
	         $user = $zendAuth->getIdentity();
	         $this->_redirect('/');
	       } 
	       else 
	       {
	         throw new Zend_Exception('Supplied credential is invalid.');
	       }
	       
	       $messages = $result->getMessages();
	       foreach($messages as $message)
	         	$this->sh_messenger->addMessage($message); 
    	}
    	else
    	{
    		
    	}
	}

   public function logoutAction()
   {
      $this->_helper->user->logout();
      $this->sh_messenger->addMessage('Logout OK.');
      //$this->ajaxLayout();
      //$this->_redirect('/');
   }
    
   public function accountAction()
   {    	
    	$this->view->section_desc = 'User Account Edit';
    	
    	
    	if(isset($this->params['saveit'])) $this->saveaccountAction($this->params);
    	
    	$user = $this->UserModel->GetUser($this->user->id);
    	$uform = new Zend_Form;
    	$uform->setAction('/account/')
     		  ->setMethod('post')
     		  ->addElement('text', 'login',array('class'=>'form','value'=>$user['login'],'label'=>'Login:','disabled'=>'disabled'))
     		  ->addElement('text', 'name',array('class'=>'form','value'=>$user['name'],'label'=>'Username:'))
     		  ->addElement('password', 'password',array('class'=>'form','value'=>'','label'=>'New Password:'))
     		  ->addElement('text', 'email',array('class'=>'form','value'=>$user['email'],'label'=>'Email:'))
     		  ->addElement('hidden', 'saveit',array('value'=>'1'))
     		  ->addElement('hidden', 'id',array('value'=>$this->user->id))
     		  ->addElement('submit', 'submit',array('class'=>'form','label'=>'Save General Settings'));
 
			$this->view->uform = $uform; 
			$this->view->user = $user; 		 

    }
    
		public function registrationAction()
		{
			if(isset($this->params['user']))
	    {
			  $params = $this->params;
	    	if($params['user']['password']=='') 
	    	    unset($params['user']['password']); 
	    	else 
	    	    $params['password'] = md5($params['user']['password']);   
	    	    
	    	$SettingsModel = new Default_Model_Settings();
	    	
	    	if($this->validate($params['user'])) 
	    	{ 
		    	$this->UserModel->register($params['user']);
		    	$this->view->message = $SettingsModel->getItem('TRUEREGMESSAGE');
	    	}
	    	else 
	    	 $this->view->message = $SettingsModel->getItem('FALSEREGMESSAGE');   
	    }
		}

    public function validate()
    {
        $this->view->assign('errors',array());
        return true;
    }
  
}

